package videos;

import example.demo_navi.R;
import gui_View.VideosListView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import models.Library;
import models.Video;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import clicks.VideoClickListener;

import utility.Check_Net;
import utility.SDialog;
import utility.StreamUtils;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class Main2 extends Activity implements VideoClickListener{

	private VideosListView vl;
	private String str;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_2);
		vl=(VideosListView) findViewById(R.id.vlv1);
	    vl.setOnVideoClickListener(this);
		Intent in=getIntent();
		str=in.getDataString();
		Log.d("Test2",str);
		if(Check_Net.isOnLine(Main2.this)){
		new Thread(new GetYouT(responseHandler, "choa38")).start();
		}else{
			SDialog.Dialognet(Main2.this);
		}
	}

	private class GetYouT implements Runnable{

		public static final String LIBRARY = "Library";
	
		private final Handler replyTo;
	
		private final String username;
		
		public GetYouT(Handler replyTo, String username) {
			this.replyTo = replyTo;
			this.username = username;
		}
		
		public void run() {
			// TODO Auto-generated method stub
			try {
				String link="PLF9CAlAz_mAJDt-FSHaLax5IOp1P4hJwB";
		
				HttpClient client = new DefaultHttpClient();
				
				HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/"+str+"?v=2&alt=jsonc");
	
				HttpResponse response = client.execute(request);

				String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
	
				JSONObject json = new JSONObject(jsonString);
				
				JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
				
				List<Video> videos = new ArrayList<Video>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					JSONObject vid=jsonObject.getJSONObject("video");
					String title = vid.getString("title");
					String url;
					try {
						url = vid.getJSONObject("content").getString("6");
					} catch (JSONException ignore) {
						url = vid.getJSONObject("content").getString("1");
					}

					String thumbUrl = vid.getJSONObject("thumbnail").getString("sqDefault");
					
					videos.add(new Video(title, url, thumbUrl));
					
				}
		
				Library lib = new Library(username, videos);
			
				Bundle data = new Bundle();
				data.putSerializable(LIBRARY, lib);
				
				Message msg = Message.obtain();
				msg.setData(data);
				replyTo.sendMessage(msg);
				
			} catch (ClientProtocolException e) {
				Log.e("Feck", e.getMessage());
			} catch (IOException e) {
				Log.e("Feck", e.getMessage());
			} catch (JSONException e) {
				Log.e("Feck", e.getMessage());
			}
		}
		
	}
	
	Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};

	private void populateListWithVideos(Message msg) {
		Library lib = (Library) msg.getData().get(GetYouT.LIBRARY);
		vl.setVideos(lib.getVideos());
	}
	
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		responseHandler=null;
		super.onStop();
	}

	public void onVideoClicked(Video video) {
		// TODO Auto-generated method stub
		Intent in=new Intent(Main2.this,Main_4.class);
		in.setData(Uri.parse(video.getUrl()));
		startActivity(in);
	//	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}
	
}
